package com.itHsuSE.houserent.service;

import com.itHsuSE.houserent.domain.House;

public class HouseService {

    private House[] houses;
    private int houseSNums=1;//记录当前有几个房屋信息
    private int idCounter=1;//记录当前id自增长到哪个值

    public HouseService(int size){
        houses=new House[size];
        houses[0]=new House(1,"jack","112","海淀区",2000,"未出租");
    }

    public boolean del(int delId){
        int index=-1;
        for (int i = 0; i < houseSNums; i++) {
            if(delId==houses[i].getId()){
                index=i;//找到要删除的房屋
            }
        }
        if(index==-1){//没找到
            return false;
        }
        for(int i=index;i<houseSNums-1;i++){//-1防止数组越界
            houses[i]=houses[i+1];//将后一个房屋往前移覆盖
        }
        houses[--houseSNums]=null;//将最后一个元素置空
        return true;
    }

    public boolean add(House newHouse){
        if(houseSNums>=houses.length){
            System.out.println("数组已满");
            return false;
        }
        houses[houseSNums++]=newHouse;
        newHouse.setId(++idCounter);//更新newHouse的id
        return true;
    }

    public House[] list(){

        return houses;
    }
}
